<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%
	String contextPath = request.getContextPath();
%>
<jsp:include page="/WEB-INF/jsp/common/fadeout.jsp"></jsp:include>
<div id="log_layout" class="easyui-layout" data-options="fit:true" style="width:600px;height:400px;">   
    <div data-options="region:'center',title:'待提交信息',split:'false'" >
    	<table id="local_cart_datagrid"><div id="subCart_tb">
    	<a href="javascript:void(0);" onclick="subCart()" class="easyui-linkbutton" data-options="iconCls:'icon-basket_add',plain:true">提交至本地配送订单</a>|
    	<a href="javascript:void(0);" onclick="delCart()" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true">删除</a></div>
    	</table>
    </div>
 </div>
  <div id="add_address" align="center">
		<table id="dg" class="easyui-datagrid" style="height:auto"
			data-options="
				iconCls: 'icon-edit',
				singleSelect: true,
				toolbar: '#tb',
				onClickRow: onClickRow
			">
		<thead>
			<tr>
				<th data-options="field:'num',width:80,editor:{type:'textbox',options:{required:true,validType:'integer',missingMessage:'请输入数量'}}">商品数量</th>
				<th data-options="field:'receiver',width:80,editor:{type:'textbox',options:{required:true,missingMessage:'请输入收货人'}}">收货人</th>
				<th data-options="field:'address',width:300,editor:{type:'textbox',options:{required:true,missingMessage:'请输入地址'}}">发货地址</th>
			</tr>
		</thead>
	</table>
		<div id="tb" style="height:auto">
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="append()">添加</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removeit()">删除</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="reject()">撤销</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="accept()">提交</a>
<!-- 		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="getChanges()">GetChanges</a> -->
	</div>
	
	<script type="text/javascript">
		var editIndex = undefined;
		function endEditing(){
			if (editIndex == undefined){return true}
			if ($('#dg').datagrid('validateRow', editIndex)){
				//var ed = $('#dg').datagrid('getEditor', {index:editIndex,field:'productid'});
				//var productname = $(ed.target).combobox('getText');
				//$('#dg').datagrid('getRows')[editIndex]['productname'] = productname;
				$('#dg').datagrid('endEdit', editIndex);
				editIndex = undefined;
				return true;
			} else {
				return false;
			}
		}
		function onClickRow(index){
			if (editIndex != index){
				if (endEditing()){
					$('#dg').datagrid('selectRow', index)
							.datagrid('beginEdit', index);
					editIndex = index;
				} else {
					$('#dg').datagrid('selectRow', editIndex);
				}
			}
		}
		function append(){
			if (endEditing()){
				$('#dg').datagrid('appendRow',{status:'P'});
				editIndex = $('#dg').datagrid('getRows').length-1;
				$('#dg').datagrid('selectRow', editIndex)
						.datagrid('beginEdit', editIndex);
			}
		}
		function removeit(){
			if (editIndex == undefined){return}
			$('#dg').datagrid('cancelEdit', editIndex)
					.datagrid('deleteRow', editIndex);
			editIndex = undefined;
		}
		/**提交*/
		function accept(){
			if (endEditing()){
				$('#dg').datagrid('acceptChanges');
			}
			var saveDataAry=new Array();  
			var rows = $("#local_cart_datagrid").datagrid('getChecked');
				myids = [];
				//前一个是本地配送车id后一个是提交产品信息id
				myids.push(rows[0].id);
				myids.push(rows[0].proInfoId);
			var addsrows = $("#dg").datagrid("getRows");
			if(addsrows.length==0){
				parent.$.messager.alert('提示', "提交内容不能为空");
				return;
			}
			for(var i=0;i<addsrows.length;i++){
				var data = {"num":addsrows[i].num,"receiver":addsrows[i].receiver,"address":addsrows[i].address};
				saveDataAry.push(data);
			}
		
			$.ajax({
					url : "repertory_manager/subToOrder.do?ids="+myids.join(","),
					type: "post",
				    dataType:"json",      
				    contentType:"application/json",               
				    data:JSON.stringify(saveDataAry), 
					success : function(data) {
						if (data && data.success) {
							if (data.msg && data.msg != "")
								parent.$.messager.alert('提示', data.msg);
							$("#add_address").dialog('close');
							$("#local_cart_datagrid").datagrid('reload');
							$("#local_cart_datagrid").datagrid('uncheckAll');// 把选择的checked记录全部清空
						} else {
							parent.$.messager.alert('错误', data.msg);
						}
					}
				});
			 if($('#tabs').tabs('exists',"我的本地配送订单")==true){
		        	$("#local_order_datagrid").datagrid('reload');
					$("#local_order_datagrid").datagrid('uncheckAll');
		    	}
		}
		function reject(){
			$('#dg').datagrid('rejectChanges');
			editIndex = undefined;
		}
		function getChanges(){
			var rows = $('#dg').datagrid('getChanges');
			alert(rows.length+' rows are changed!');
		}
	</script>	
<!--   <a href="javascript:void(0);" onclick="subToLocal()" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">确定</a>&nbsp;&nbsp;&nbsp; -->
 </div>
  <input type="hidden" name="filePath2" value="${filePath2}"/> 
 <script>
 //var myids = [];
 $(function(){
		$("#local_cart_datagrid").datagrid({    
		    url:'repertory_manager/allCarts.do', 
		    loadMsg:'数据加载中···',
		    fit:'true',
		    idField:'id',
		    pagination:true,
		    checkOnSelect : true,
			selectOnCheck : true,
		    columns:[[
		    {
				field : "ck",
				checkbox : true
			}, {
				field : "id",
				hidden : true
			}, {
				field : "proInfoId",
				hidden : true
			},     
		    {
				field:'pro_no',
				title:'商品编码',
				width:200,
				align:'center',
		    },     
		    {
				field:'pro_name',
				title:'商品名称',
				width:200,
				align:'center',
		    },
		     {
		    	 field:'warehouse',
		    	 title:'有效仓库',
		    	 width:180,
		    	 align:'center'
		    },{
		    	 field:'location',
		    	 title:'区位',
		    	 width:160,
		    	 align:'center'
		    },{
		    	 field:'loc_no',
		    	 title:'库位号',
		    	 width:160,
		    	 align:'center'
		    },{
		    	 field:'image_name',
		    	 title:'图片',
		    	 width:180,
		    	 align:'center',
		    	 formatter:function(value,row,inedex){
		    		 if(value==null || value==""){
		    			 return "未上传";
		    		 }else{
		    			 return "<a target='_blank' href="+$("input[name='filePath2']").val()+value+">点击查看</a>"; 
		    		 }
		    		 
		    	 }
		    }
		    ]],
		    fitColumns:true,
		    toolbar:'#subCart_tb'
		});  	
	});
 function getTime(old) {
		var date = new Date(old);
		var year = date.getFullYear();
		var month = date.getMonth() + 1;
		if (month < 10)
			month = "0" + month;
		var day = date.getDate();
		if (day < 10)
			day = "0" + day;
		var hour = date.getHours();
		if (hour < 10)
			hour = "0" + hour;
		var minute = date.getMinutes();
		if (minute < 10)
			minute = "0" + minute;
		var seconds = date.getSeconds();
		if (seconds < 10)
			seconds = "0" + seconds;
		var newDate = year + "-" + month + "-" + day + " " + hour + ":" + minute
				+ ":" + seconds;
		return newDate;
	}
 function subCart(){
		var rows = $("#local_cart_datagrid").datagrid('getChecked');
		if (rows && rows.length == 1) {
					myids = [];
					//前一个是本地配送车id后一个是提交产品信息id
					for ( var i = 0; i < rows.length; i++) {
						myids.push(rows[i].id);
						myids.push(rows[i].proInfoId);
					}
					add_address();
		} else {
			parent.$.messager.alert('提示', "请选择一条需要提交的商品！");
		}
	}
 /**删除信息*/
 function delCart(){
 	var rows = $("#local_cart_datagrid").datagrid('getChecked');
 	if (rows && rows.length > 0) {
 		parent.$.messager.confirm('提示', '是否删除该记录?', function(r) {
 			if (r) {
 				var ids = [];
 				for ( var i = 0; i < rows.length; i++) {
 					ids.push(rows[i].id);
 				}
 				$.ajax({
 					url : "repertory_manager/delCart.do?ids="+ids.join(","),
 					dataType : "json",
 					success : function(data) {
 						if (data && data.success) {
 							if (data.msg && data.msg != "")
 								parent.$.messager.alert('提示', data.msg);
 							else
 								parent.$.messager.alert('提示', "删除成功");
 							$("#local_cart_datagrid").datagrid('reload');
 							$("#local_cart_datagrid").datagrid('uncheckAll');// 把选择的checked记录全部清空
 						} else {
 							parent.$.messager.alert('错误', data.msg);
 						}
 			}
 		});
 			}});
 	} else {
 		parent.$.messager.alert('提示', "请选择需要删除的记录！");
 	}
 }
 function add_address(){
	 $('#dg').datagrid('loadData', { total: 0, rows: [] });
	   $("#add_address").dialog({    
	    title: '收货地址填写',    
	    width: 480,   
	    height: 500,    
	    closed: false,    
	    cache: false,
	    iconCls:'icon-lorry',
	    modal: true   
	});  
}
 function subToLocal(){
	 //var address = $("#address").val();
	 var address= encodeURI(encodeURI($("#address").val()));
	 if(address==null || address==""){
			parent.$.messager.alert('提示', "请输入收货地址！");
		}else{
			$("#add_address").dialog('close');
			$.ajax({
				url : "repertory_manager/subToOrder.do?ids="+myids.join(",")+"&address="+address,
				dataType : "json",
				type: "post",
				success : function(data) {
					if (data && data.success) {
						if (data.msg && data.msg != "")
							parent.$.messager.alert('提示', data.msg);
						$("#local_cart_datagrid").datagrid('reload');
						$("#local_cart_datagrid").datagrid('uncheckAll');// 把选择的checked记录全部清空
					} else {
						parent.$.messager.alert('错误', data.msg);
					}
				}
			});
		}
 }
 </script>
